#include "uart.h"
#include "lvgl.h"
#include "string.h"
#include "lvgl_main.h"


#define  PROTOCOL_HEAD_FIR  0xAB
#define  PROTOCOL_HEAD_SEC  0x85
#define  PROTOCOL_HEAD_LEN  2
#define  PROTOCOL_CMD_LEN   1
#define  PROTOCOL_DATE_LEN  1
#define  PROTOCOL_SUM_LEN   1
#define  PROTOCOL_CMD_START 0xB0
#define  PROTOCOL_CMD_SEND  0xB1
#define  PROTOCOL_CMD_END   0xB2


void hex_to_ascii(char *out, const uint8_t *in, uint16_t in_len) {
    uint16_t m_loop;
    for (m_loop = 0; m_loop < in_len; m_loop++) {
        out[m_loop] = (char)in[m_loop];
    }
    out[in_len] = '\0';
}

uint8_t uart_app_get_sum(uint8_t *date,uint16_t len)
{
    uint16_t m_loop;
    uint8_t sum = 0;

    for(m_loop = 0;m_loop<len;m_loop++) {
        sum += date[m_loop];
    }
    sum = ~sum;
//    printf("\r\n ~sum = 0x%x\r\n",sum);
    return sum;
}

/* get date and save file*/
uint8_t uart_app_protocol(uint8_t *date,uint16_t len)
{
    uint8_t ret = 0;
    uint8_t date_len = 0;
    uint8_t m_file_name[32];
    uint8_t m_file_name_all[32] = {0};
    static lv_fs_file_t m_write_file;
    static uint32_t m_file_point = 0;
    uint32_t m_have_write = 0;
    uint8_t *date_start = date + PROTOCOL_HEAD_LEN + PROTOCOL_CMD_LEN + PROTOCOL_DATE_LEN;
//    printf("uart_app_protocol:\r\n");
//    for (ret = 0;ret<len;ret++)
//        printf("0x%x,",date[ret]);
//    printf("\r\n");

    if ( date[0] == PROTOCOL_HEAD_FIR && date[1] == PROTOCOL_HEAD_SEC) //Head
    {
        switch(date[2]) { //cmd
        case PROTOCOL_CMD_START: //get file name
            if (len > (PROTOCOL_HEAD_LEN + PROTOCOL_CMD_LEN) )date_len = date[3]; //len
            if (date_len > 32) {
                printf("fine name is too long\r\n");
                ret = 1;
                break;
            }
            if (uart_app_get_sum(date_start,date_len)== date[len - 1]) { // if check sum is ok ,data is right
                hex_to_ascii(m_file_name,date_start,date_len);
            }
//            printf("file name = %s\r\n",m_file_name);
            if (strlen(m_file_name)) { //success get file name ,
                lvgl_main_get_dir_and_file_status(m_file_name,1);
                sprintf(m_file_name_all, "A:%s",m_file_name);
//                printf("file name all= %s\r\n",m_file_name_all);
                ret = lv_fs_open(&m_write_file, m_file_name_all, LV_FS_MODE_WR);//open file for ready write file ,if success return 0
//                printf("open  file ok\r\n");
//                printf("ret = %d\r\n",ret);
                m_file_point = 0;
            } else {
                 ret = 1;
             }
            break;
        case PROTOCOL_CMD_SEND: //sending write file
            date_len = date[3]; //len
            if (uart_app_get_sum(date_start,date_len)== date[len - 1]) { // if check sum is ok ,data is right
                lv_fs_seek(&m_write_file,m_file_point,LV_FS_SEEK_SET);
                lv_fs_write(&m_write_file, date_start, date_len, &m_have_write);
                m_file_point += m_have_write;
//                printf("m_file_point = %d\r\n",m_file_point);
                ret = 0;
            } else { //date is error
                ret = 1;
            }
            break;
        case PROTOCOL_CMD_END: //end write file
            date_len = date[3]; //len
            if (uart_app_get_sum(date_start,date_len)== date[len - 1]) { // if check sum is ok ,data is right
                lv_fs_seek(&m_write_file,m_file_point,LV_FS_SEEK_SET);
                lv_fs_write(&m_write_file, date_start, date_len, &m_have_write);
                lv_fs_close(&m_write_file);
//                printf("close file ok\r\n");
                ret = 0;
            } else { //date is error
                lv_fs_close(&m_write_file);
                ret = 1;
            }
            break;
        }
    }


    return ret;

}

void uart_app_send_protocol_result(uint8_t result)
{
    uint8_t m_send_date[6] = {0};
    m_send_date[0] = PROTOCOL_HEAD_FIR;
    m_send_date[1] = PROTOCOL_HEAD_SEC;
    m_send_date[2] = PROTOCOL_CMD_START;
    m_send_date[3] = 0x01; //date len

    if (result) { //send success signal
        m_send_date[4] = 0x01;
    } else {
        m_send_date[4] = 0x00;
    }
    m_send_date[5] = ~m_send_date[4];
    uart_hardware_uart4_send_date(m_send_date,6);

}
